﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using backoffice.Data;
using System.Linq.Expressions;

namespace backoffice.Business
{
    public class BusinessDisease
    {
        #region Create
        public static void createDisease(Disease d)
        {
            try
            {
                dataDisease.createDisease(d);
            }
            catch (Exception e)
            {
                Messages.AddDebug(e.Message);
                throw new Exception("Error creating disease");
            }
        }

        public static void createDisease(String name, String description)
        {
            try
            {
                Disease d = new Disease();
                d.Name = name;
                d.Description = description;

                dataDisease.createDisease(d);
            }
            catch (Exception e)
            {
                Messages.AddDebug(e.Message);
                throw new Exception("Error creating disease");
            }
        }
        #endregion

        #region Get
        public static Disease getDiseases(Guid guid)
        {
            try
            {
                return dataDisease.getDisease(guid);
            }
            catch (Exception e)
            {
                Messages.AddDebug(e.Message);
                throw new Exception("Error getting disease");
            }
        }

        public static List<Plant> getPlantsByDisease(Disease d)
        {
            try
            {
                List<Plant> plants = new List<Plant>();

                Expression<Func<Plants_Disease, bool>> whereClause = (Plants_Disease p_d) => (p_d.DiseaseID == d.DiseaseID);
                List<Plants_Disease> pd = dataDisease.getDiseases_Plant(whereClause).ToList();

                foreach (Plants_Disease p_d in pd)
                {
                    Plant p = dataPlant.getPlant(p_d.PlantID);

                    if (!plants.Contains(p))
                    {
                        plants.Add(p);
                    }
                }

                return plants;
            }
            catch (Exception e)
            {
                Messages.AddDebug(e.Message);
                throw new Exception("Error getting plants by disease");
            }
        }
        #endregion

        #region Set
        public static Boolean changeDisease(Disease d)
        {
            try
            {
                return dataDisease.changeDisease(d);
            }
            catch (Exception e)
            {
                Messages.AddDebug(e.Message);
                throw new Exception("Error changing disease");
            }
        }
        #endregion

        #region Delete
        public static Boolean deleteDisease(Disease d)
        {
            try
            {
                return dataDisease.deleteDisease(d);
            }
            catch (Exception e)
            {
                Messages.AddDebug(e.Message);
                throw new Exception("Error deletting disease");
            }
        }
        #endregion
    }
}
